home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 422_02 / misc / rain.c < prev    next >
C/C++ Source or Header  |  1994-03-20  |  2KB  |  87 lines

  1. /*
  2.  * Rainy prompt.
  3.  *
  4.  * This program causes the characters on the screen to 'rain' down, and
  5.  * pile up at the bottom of the screen. The original screen is saved, and
  6.  * restored when the program terminates. It makes a novel "delay" function
  7.  * for "batch" files.
  8.  *
  9.  * Arguments are (# of raindrops), and an optional "prompt" string.
  10.  * Returns an ERRORLEVEL of zero (0), is a key was pressed to exit,
  11.  * or -1 if all drops fell without keyboard action.
  12.  *
  13.  * Copyright 1994 Dave Dunfield
  14.  * All rights reserved.
  15.  *
  16.  * Permission granted for personal (non-commercial) use only.
  17.  *
  18.  * Compile command: cc rain -fop
  19.  */
  20. #include <stdio.h>
  21. #include <window.h>
  22.  
  23. static unsigned bottom = 24*160;
  24.  
  25. main(argc, argv)
  26.     int argc;
  27.     char *argv[];
  28. {
  29.     int i;
  30.  
  31.     if(argc < 2)
  32.         abort("Use: rain <drops> [prompt text]");
  33.  
  34. /* Open window, obtain arguments & make rain */
  35.     wopen(0, 0, 80, 25, WSAVE|REVERSE);
  36.     wcursor_off();
  37.     if(argc > 2) {        /* Prompt text given */
  38.         bottom = 23*160;
  39.         wgotoxy(0, 24);
  40.         for(i=2; i < argc; ++i)
  41.             wprintf("%s ", argv[i]);
  42.         wcleol(); }
  43.     i = atoi(argv[1]);
  44.     while(i--) {
  45.         if(drop_column(rand(80))) {
  46.             wclose();
  47.             exit(0); } }
  48.     wclose();
  49.     exit(-1);
  50. }
  51.  
  52. /*
  53.  * Locate the lowest character in a specific screen column, which is not
  54.  * already resting on the bottom, and drop it down.
  55.  */
  56. drop_column(x)
  57.     int x;
  58. {
  59.     x = (x + x) + bottom;
  60.  
  61.     /* Locate the lowest blank character first */
  62.     while(x >= 0) {
  63.         if(peek(W_BASE, x) == ' ')
  64.             goto found1;
  65.         x -= 160; }
  66.     /* No space to drop in this column */
  67.     return wtstc();
  68.  
  69. found1: /* Locate a higher character to 'drop' */
  70.     while(x >= 0) {
  71.         if(peek(W_BASE, x) != ' ')
  72.             goto found2;
  73.         x -= 160; }
  74.     /* No character to move at this column */
  75.     return wtstc();
  76.  
  77. found2: /* Lower character to bottom of screen */
  78.     while((x < bottom) && (peek(W_BASE, x+160) == ' ')) {
  79.         poke(W_BASE, x+160, peek(W_BASE, x));
  80.         poke(W_BASE, x, ' ');
  81.         delay(50);
  82.         x += 160;
  83.         if(wtstc())
  84.             return 1; }
  85.     return 0;
  86. }
  87.